//
//  DestinationPlaceViewController.m
//  HappyTrip
//
//  Created by yuxuesong on 15-3-23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DestinationPlaceViewController.h"
#import "DestinationDetailObject.h"
#import "DesPlacePhotoObject.h"
#import "StarsView.h"
#import "DesSmallPhotosCVC.h"
#import "CircleLayout.h"
#import "DesSmallPhoto_albumObject.h"
#import "DesTripPhotosVController.h"
#import "DesPosterVC.h"
#import "DesDataObj.h"

#define KCellIdentifier @"identifier"
#define WORDSIZE 13
#define LABELOUTBOUNDS 20

@interface DestinationPlaceViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain)NSString *urlStr;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)NSMutableArray *placePhotoArr;
@property (nonatomic, retain)NSMutableArray *detailModelArr;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *detailPhotosArr;
@property (nonatomic, retain)NSMutableArray *smallPhoto_albumArr;

@property (nonatomic, retain)DestinationDetailObject *detailModel;
@property (nonatomic, retain)DesPlacePhotoObject *placePhotoModel;
@property (nonatomic, retain)StarsView *starsView;
@property (nonatomic, retain)DesSmallPhoto_albumObject *smallPhoto_albumModel;

@property (nonatomic, retain)UIImageView *imgProfile;
@property (nonatomic, retain)UILabel *titleLabel;
@property (nonatomic, retain)UILabel *starsWordLabel;
@property (nonatomic, retain)UILabel *reasonLabel;
@property (nonatomic, retain)UILabel *wordLabel;
@property (nonatomic, retain)UILabel *place_desciptionLabel; // 概览
@property (nonatomic, retain)UILabel *addressLabel; // 地址
@property (nonatomic, retain)UILabel *arrival_typeLabel; // 到达方式
@property (nonatomic, retain)UILabel *opening_timeLabel; // 开放时间
@property (nonatomic, retain)UILabel *feeLabel; // 门票
@property (nonatomic, retain)UILabel *telLabel; // 联系方式
@property (nonatomic, retain)UILabel *websiteLabel; // 网址

@property (nonatomic, retain)UIImageView *recommend_reasonImage;
@property (nonatomic, retain)UIImageView *place_desciptionImage;
@property (nonatomic, retain)UIImageView *addressImage;
@property (nonatomic, retain)UIImageView *arrival_typeImage;
@property (nonatomic, retain)UIImageView *opening_timeImage;
@property (nonatomic, retain)UIImageView *feeImage;
@property (nonatomic, retain)UIImageView *telImage;
@property (nonatomic, retain)UIImageView *websiteImage;

@property (nonatomic, retain)UIButton *collectButton;
@property (nonatomic, retain)UIButton *shareButton;

@property (nonatomic, retain)UIAlertView *alertView;

@property (nonatomic, assign) BOOL flag;

@end

@implementation DestinationPlaceViewController

- (void)dealloc
{
    [_urlStr release];
    [_placeSendID release];
    [_placeSendType release];
    [_scrollView release];
    [_placePhotoArr release];
    [_detailModelArr release];
    [_smallPhoto_albumArr release];
    [_collectionView release];

    [_detailModel release];
    [_placePhotoModel release];
    [_starsView release];
    [_smallPhoto_albumModel release];
    
    [_imgProfile release];
    [_titleLabel release];
    [_starsWordLabel release];
    [_reasonLabel release];
    [_wordLabel release];
    [_place_desciptionLabel release];
    [_addressLabel release];
    [_arrival_typeLabel release];
    [_opening_timeLabel release];
    [_feeLabel release];
    [_telLabel release];
    [_websiteLabel release];
    
    [_recommend_reasonImage release];
    [_place_desciptionImage release];
    [_addressImage release];
    [_arrival_typeImage release];
    [_opening_timeImage release];
    [_feeImage release];
    [_telImage release];
    [_websiteImage release];
    [_alertView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 / 255.0 green:170.0 / 255.0 blue:205.0 / 255.0 alpha:1.0];
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
    
    [self handleData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorFromHexCode:@"#F5FFFA"];
    
    [self setupViews];
    
    
}


- (void)setupViews
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont_shoucangicon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(collectButtonAction:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style: UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    self.imgProfile = [[UIImageView alloc] init];
    [self.scrollView addSubview:self.imgProfile];
    [_imgProfile release];
    
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.imgProfile addGestureRecognizer:tapRecognizer];
    [tapRecognizer release];
    
    self.titleLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_titleLabel];
    [_titleLabel release];
    
    self.starsWordLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_starsWordLabel];
    [_starsWordLabel release];
    
    self.recommend_reasonImage = [[UIImageView alloc] init];
    [self.scrollView addSubview:_recommend_reasonImage];
    [_recommend_reasonImage release];
    
    self.reasonLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_reasonLabel];
    [_reasonLabel release];
    
//----------  以下部分是基本信息列表
    self.wordLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_wordLabel];
    [_wordLabel release];

    self.place_desciptionLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_place_desciptionLabel];
    [_place_desciptionLabel release];
    self.place_desciptionImage = [[UIImageView alloc] init];
    [self.scrollView addSubview:_place_desciptionImage];
    [_place_desciptionImage release];
    
    self.addressLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_addressLabel];
    [_addressLabel release];
    self.addressImage = [[UIImageView alloc] init];
    [self.scrollView addSubview:self.addressImage];
    [_addressImage release];
    
    self.arrival_typeLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_arrival_typeLabel];
    [_arrival_typeLabel release];
    self.arrival_typeImage = [[UIImageView alloc] init];
    [self.scrollView addSubview:_arrival_typeImage];
    [_arrival_typeImage release];
    
    self.opening_timeLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_opening_timeLabel];
    [_opening_timeLabel release];
    self.opening_timeImage = [[UIImageView alloc] init];
    [self.scrollView addSubview:_opening_timeImage];
    [_opening_timeImage release];
    
    self.feeLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_feeLabel];
    [_feeLabel release];
    self.feeImage = [[UIImageView alloc] init];
    [self.scrollView addSubview:_feeImage];
    [_feeImage release];
    
    self.telLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_telLabel];
    [_telLabel release];
    self.telImage = [[UIImageView alloc] init];
    [self.scrollView addSubview:_telImage];
    [_telImage release];
    
    self.websiteLabel = [[UILabel alloc] init];
    [self.scrollView addSubview:_websiteLabel];
    [_websiteLabel release];
    self.websiteImage = [[UIImageView alloc] init];
    [self.scrollView addSubview:_websiteImage];
    [_websiteImage release];
}

- (void)makeViewsFrame
{
    self.title = self.detailModel.name;

    self.scrollView.frame = self.view.bounds;
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.delegate = self;
    
    self.imgProfile.frame = CGRectMake(0, 0, VIEW_WIDTH, (VIEW_HEIGHT + 64 + 49) / 3);
    self.imgProfile.contentMode = UIViewContentModeScaleAspectFill;
    self.imgProfile.clipsToBounds = YES;
    self.imgProfile.userInteractionEnabled = YES;
    self.imgProfile.backgroundColor = [UIColor colorWithRed:204.0 / 255.0 green:204.0 / 255.0 blue:159.0 / 255.0 alpha:1.0];
    [self.imgProfile setImageWithURL:[NSURL URLWithString:self.placePhotoModel.photo] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
    
    self.titleLabel.frame = CGRectMake(0, (VIEW_HEIGHT + 64 + 49) / 3 + LABELOUTBOUNDS, VIEW_WIDTH, (VIEW_HEIGHT) / 18);
    self.titleLabel.text = self.detailModel.name;
    self.titleLabel.font = [UIFont systemFontOfSize:20];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.starsWordLabel.frame = CGRectMake(0, self.titleLabel.frame.size.height + self.titleLabel.frame.origin.y + LABELOUTBOUNDS / 2, VIEW_WIDTH / 2, 14);
    self.starsWordLabel.text = @"推荐指数:";
    self.starsWordLabel.font = [UIFont systemFontOfSize:14];
    self.starsWordLabel.textAlignment = NSTextAlignmentRight;
    
    NSInteger arcStars_number = arc4random() % (50 - 20 + 1) + 20;
    self.starsView = [[StarsView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 2, self.titleLabel.frame.size.height + self.titleLabel.frame.origin.y + LABELOUTBOUNDS / 2, 5 * 14, 14)];
    [self.starsView getWithStars:[NSString stringWithFormat:@"%ld", arcStars_number]];
    [self.scrollView addSubview:_starsView];
    [_starsView release];
    

    self.recommend_reasonImage.frame = CGRectMake(LABELOUTBOUNDS / 5, self.starsWordLabel.frame.origin.y + self.starsWordLabel.frame.size.height + LABELOUTBOUNDS / 2, 14, 14);
    [self.recommend_reasonImage setImage:[UIImage imageNamed:@"recommend_reason.png"]];
    
    self.reasonLabel.frame = CGRectMake(LABELOUTBOUNDS, self.starsWordLabel.frame.origin.y + self.starsWordLabel.frame.size.height + LABELOUTBOUNDS / 2, VIEW_WIDTH - LABELOUTBOUNDS * 2,VIEW_HEIGHT);
    if (self.detailModel.recommended_reason.length != 0) {
        self.reasonLabel.text = [NSString stringWithFormat:@"推荐理由:%@", self.detailModel.recommended_reason];
    } else {
        self.reasonLabel.text = @"推荐理由:     暂无相关信息";
    }
    self.reasonLabel.font = [UIFont systemFontOfSize:14];
    self.reasonLabel.numberOfLines = 0;
    [self.reasonLabel sizeToFit];
    
    self.collectionView.frame = CGRectMake(0, self.reasonLabel.frame.origin.y + self.reasonLabel.frame.size.height + LABELOUTBOUNDS / 2, VIEW_WIDTH, (VIEW_HEIGHT + 64 + 49) / 3);
    
    self.wordLabel.frame = CGRectMake(0, self.reasonLabel.frame.origin.y + self.reasonLabel.frame.size.height +  + self.collectionView.frame.size.height + LABELOUTBOUNDS / 2, VIEW_WIDTH, 14);
    self.wordLabel.text = @"基本信息";
    self.wordLabel.textAlignment = NSTextAlignmentCenter;
    
    self.place_desciptionLabel.frame = CGRectMake(LABELOUTBOUNDS, self.wordLabel.frame.origin.y + self.wordLabel.frame.size.height + LABELOUTBOUNDS / 2, VIEW_WIDTH - LABELOUTBOUNDS * 2, VIEW_HEIGHT);
    if (self.detailModel.place_description.length != 0) {
        self.place_desciptionLabel.text = [NSString stringWithFormat:@"概要:%@", self.detailModel.place_description];
        
    } else {
        self.place_desciptionLabel.text = @"概要:  暂无相关信息";
    }
    self.place_desciptionLabel.font = [UIFont systemFontOfSize:WORDSIZE];
    self.place_desciptionLabel.numberOfLines = 0;
    [self.place_desciptionLabel sizeToFit];
    self.place_desciptionImage.frame = CGRectMake(LABELOUTBOUNDS / 5, self.wordLabel.frame.origin.y + self.wordLabel.frame.size.height + LABELOUTBOUNDS / 2, WORDSIZE, WORDSIZE);
    [self.place_desciptionImage setImage:[UIImage imageNamed:@"description.png"]];
    
    self.addressLabel.frame = CGRectMake(LABELOUTBOUNDS, self.place_desciptionLabel.frame.origin.y + self.place_desciptionLabel.frame.size.height + 10, VIEW_WIDTH - WORDSIZE * 2, VIEW_HEIGHT);
    if (self.detailModel.address.length != 0) {
        self.addressLabel.text = [NSString stringWithFormat:@"地址:%@", self.detailModel.address];
    } else {
        self.addressLabel.text = @"地址:   暂无相关信息";
    }
    self.addressLabel.font = [UIFont systemFontOfSize:WORDSIZE];
    self.addressLabel.numberOfLines = 0;
    [self.addressLabel sizeToFit];
    self.addressImage.frame = CGRectMake(LABELOUTBOUNDS / 5, self.place_desciptionLabel.frame.origin.y + self.place_desciptionLabel.frame.size.height + 10, WORDSIZE, WORDSIZE);
    [self.addressImage setImage:[UIImage imageNamed:@"address.png"]];
    
    self.arrival_typeLabel.frame = CGRectMake(LABELOUTBOUNDS, self.addressLabel.frame.origin.y + self.addressLabel.frame.size.height + 10, VIEW_WIDTH - LABELOUTBOUNDS * 2, VIEW_HEIGHT);
    if (self.detailModel.arrival_type.length != 0) {
        self.arrival_typeLabel.text = [NSString stringWithFormat:@"到达方式:%@", self.detailModel.arrival_type];
    } else {
        self.arrival_typeLabel.text = @"到达方式:   暂无相关信息";
    }
    self.arrival_typeLabel.font = [UIFont systemFontOfSize:WORDSIZE];
    self.arrival_typeLabel.numberOfLines = 0;
    [self.arrival_typeLabel sizeToFit];
    self.arrival_typeImage.frame = CGRectMake(LABELOUTBOUNDS / 5, self.addressLabel.frame.origin.y + self.addressLabel.frame.size.height + 10, WORDSIZE, WORDSIZE);
    [self.arrival_typeImage setImage:[UIImage imageNamed:@"arrival_type.png"]];
    
    self.opening_timeLabel.frame = CGRectMake(LABELOUTBOUNDS, self.arrival_typeLabel.frame.origin.y + self.arrival_typeLabel.frame.size.height + 10, VIEW_WIDTH - LABELOUTBOUNDS * 2, VIEW_HEIGHT);
    if (self.detailModel.opening_time.length != 0) {
        self.opening_timeLabel.text = [NSString stringWithFormat:@"开放时间:%@", self.detailModel.opening_time];
    } else {
        self.opening_timeLabel.text = @"开放时间:   全天";
    }
    self.opening_timeLabel.font = [UIFont systemFontOfSize:WORDSIZE];
    self.opening_timeLabel.numberOfLines = 0;
    [self.opening_timeLabel sizeToFit];
    
    self.opening_timeImage.frame = CGRectMake(LABELOUTBOUNDS / 5, self.arrival_typeLabel.frame.origin.y + self.arrival_typeLabel.frame.size.height + 10, WORDSIZE, WORDSIZE);
    [self.opening_timeImage setImage:[UIImage imageNamed:@"opening_time.png"]];
    
    self.feeLabel.frame = CGRectMake(LABELOUTBOUNDS, self.opening_timeLabel.frame.origin.y + self.opening_timeLabel.frame.size.height + 10, VIEW_WIDTH - LABELOUTBOUNDS * 2, VIEW_HEIGHT);
    if (self.detailModel.fee.length != 0) {
        self.feeLabel.text = [NSString stringWithFormat:@"门票:%@", self.detailModel.fee];
    } else {
        self.feeLabel.text = @"门票:   免费";
    }
    self.feeLabel.font = [UIFont systemFontOfSize:WORDSIZE];
    self.feeLabel.numberOfLines = 0;
    [self.feeLabel sizeToFit];
    self.feeImage.frame = CGRectMake(LABELOUTBOUNDS / 5, self.opening_timeLabel.frame.origin.y + self.opening_timeLabel.frame.size.height + 10, WORDSIZE, WORDSIZE);
    [self.feeImage setImage:[UIImage imageNamed:@"fee.png"]];

    self.telLabel.frame = CGRectMake(LABELOUTBOUNDS, self.feeLabel.frame.origin.y + self.feeLabel.frame.size.height + 10, VIEW_WIDTH - LABELOUTBOUNDS * 2, VIEW_HEIGHT);
    if (self.detailModel.tel.length != 0) {
        self.telLabel.text = [NSString stringWithFormat:@"联系方式:%@", self.detailModel.tel];
    } else {
        self.telLabel.text = @"联系方式:    暂无相关信息";
    }
    self.telLabel.font = [UIFont systemFontOfSize:WORDSIZE];
    self.telLabel.numberOfLines = 0;
    [self.telLabel sizeToFit];
    self.telImage.frame = CGRectMake(LABELOUTBOUNDS / 5, self.feeLabel.frame.origin.y + self.feeLabel.frame.size.height + 10, WORDSIZE, WORDSIZE);
    [self.telImage setImage:[UIImage imageNamed:@"tel.png"]];

    self.websiteLabel.frame = CGRectMake(LABELOUTBOUNDS, self.telLabel.frame.origin.y + self.telLabel.frame.size.height + 10, VIEW_WIDTH - LABELOUTBOUNDS * 2, VIEW_HEIGHT);
    if (self.detailModel.website.length != 0) {
        self.websiteLabel.text = [NSString stringWithFormat:@"网址:%@", self.detailModel.website];
    } else {
        self.websiteLabel.text = @"网址:  暂无相关信息";
    }
    self.websiteLabel.font = [UIFont systemFontOfSize:WORDSIZE];
    self.websiteLabel.numberOfLines = 0;
    [self.websiteLabel sizeToFit];
    self.websiteImage.frame = CGRectMake(LABELOUTBOUNDS / 5, self.telLabel.frame.origin.y + self.telLabel.frame.size.height + 10, WORDSIZE, WORDSIZE);
    [self.websiteImage setImage:[UIImage imageNamed:@"website.png"]];
    
     self.scrollView.contentSize = CGSizeMake(VIEW_WIDTH, self.websiteLabel.frame.origin.y + self.websiteLabel.frame.size.height + LABELOUTBOUNDS / 2);
}

- (void)valueMethod
{
    [self makeViewsFrame];
    DesDataObj *db = [DesDataObj shareInstance];
    NSMutableArray *arr = [db selectAllSpots];
    NSMutableArray *dbArr = [NSMutableArray array];
    for (NSInteger i = 0; i < arr.count; i++) {
        NSString *name = [[arr objectAtIndex:i] valueForKey:@"name"];
        [dbArr addObject:name];
    }
    if ([dbArr containsObject:self.titleLabel.text]) {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-yijingshoucang.png"]];
    } else {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-shoucangicon.png"]];
    }
}

#pragma mark - 小图册 collectionView
- (void)setupCollectionView
{
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.reasonLabel.frame.origin.y + self.reasonLabel.frame.size.height + LABELOUTBOUNDS / 2, VIEW_WIDTH, (VIEW_HEIGHT) / 3) collectionViewLayout:[[CircleLayout alloc] init]];
    [self.collectionView registerClass:[DesSmallPhotosCVC class] forCellWithReuseIdentifier:KCellIdentifier];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.scrollView addSubview:self.collectionView];
//    [_collectionView release];
    
    [CircleLayout release];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView == _scrollView) {
        //------ 上端图片下拉放大
        CGFloat yOffset   = self.scrollView.contentOffset.y;
        if (yOffset < 0) {
            CGFloat factor = ((ABS(yOffset)+(VIEW_HEIGHT + 64 + 49) / 3) * VIEW_WIDTH) / ((VIEW_HEIGHT + 64 + 49) / 3);
            CGRect f = CGRectMake(- (factor-VIEW_WIDTH) / 2, 0, factor, (VIEW_HEIGHT + 64 + 49) / 3 + ABS(yOffset));
            self.imgProfile.frame = f;
            _imgProfile.center = CGPointMake(_imgProfile.center.x, _imgProfile.center.y - ABS(yOffset));
        }
    } else {
        //---- 小图册无限循环....
        float targetX = scrollView.contentOffset.x;
        NSInteger numCount = [self.collectionView numberOfItemsInSection:0];
        float ITEM_WIDTH = scrollView.frame.size.width;
        if (numCount>=3)
        {
            if (targetX < ITEM_WIDTH/2) {
                [scrollView setContentOffset:CGPointMake(targetX+ITEM_WIDTH *numCount, 0)];
            }
            else if (targetX >ITEM_WIDTH/2+ITEM_WIDTH *numCount)
            {
                [scrollView setContentOffset:CGPointMake(targetX-ITEM_WIDTH *numCount, 0)];
            }
        }
    }
}
#pragma mark - 收藏按钮
- (void)collectButtonAction:(UIBarButtonItem *)button
{
    DesDataObj *db = [DesDataObj shareInstance];
    DestinationDetailObject *detailData = [[DestinationDetailObject alloc] init];
    detailData.idNumber = self.placeSendID;
    detailData.typeNumber = self.placeSendType;
    detailData.name = self.titleLabel.text;
    self.alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    if ([self.navigationItem.rightBarButtonItem.image isEqual:[UIImage imageNamed:@"iconfont-shoucangicon.png"]]) {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-yijingshoucang.png"]];
        [db insertSpot:detailData];
        [self.alertView setTitle:@"已成功收藏"];
        [self.alertView show];
        [self.alertView release];
    } else {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-shoucangicon.png"]];
        [db deleteSpot:detailData];
        [self.alertView setTitle:@"已取消收藏"];
        [self.alertView show];
        [self.alertView release];
    }
    
    [NSTimer scheduledTimerWithTimeInterval:1.2f target:self selector:@selector(dismissAction:) userInfo:nil repeats:NO];
    
}
- (void)dismissAction:(NSTimer *)timer
{
    [self.alertView dismissWithClickedButtonIndex:0 animated:YES];
}
#pragma mark - 左侧返回按钮
- (void)leftBarButtonItemAction:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - collectionView 的协议方法们
//--------  UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.smallPhoto_albumArr.count > 10) {
        return 10;
    } else {
        return self.smallPhoto_albumArr.count;
    }
}
//------  UICollectionViewDataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DesSmallPhotosCVC *cell = (DesSmallPhotosCVC *)[collectionView dequeueReusableCellWithReuseIdentifier:KCellIdentifier forIndexPath:indexPath];
    cell.smallPhoto_albumObject = [self.smallPhoto_albumArr objectAtIndex:indexPath.row];
    return cell;
}
//------- 点击 push 下一界面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DesPosterVC *posterVC = [[DesPosterVC alloc] init];
    posterVC.posterSendId = self.placeSendID;
    posterVC.posterSendType = self.placeSendType;
//    posterVC.indexNumber = indexPath.item;
    [self.navigationController pushViewController:posterVC animated:YES];
    [posterVC release];
}

//--------- 页面上端图片 点击手势
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    
    DesTripPhotosVController *phtotsVCell = [[DesTripPhotosVController alloc] init];
    phtotsVCell.photosSendId = self.placeSendID;
    phtotsVCell.photosSendType = self.placeSendType;
    [self.navigationController pushViewController:phtotsVCell animated:YES];
    [phtotsVCell release];
}

- (void)handleData
{
    self.urlStr =[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", self.placeSendType, self.placeSendID];
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [NSString stringWithFormat:@"%@/%ld.happyTripPlace", docPath, [self.urlStr hash]];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:self.urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        BOOL result = [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
        NSLog(@"存储成功: %d", result);
        self.detailModel = [[DestinationDetailObject alloc] initWithDic:responseObject];
        self.detailModelArr = [NSMutableArray array];
        [self.detailModelArr addObject:self.detailModel];
        NSMutableArray *placesArr = [responseObject objectForKey:@"hottest_places"];
        self.placePhotoArr = [NSMutableArray array];
        for (NSMutableDictionary *photoDic in placesArr) {
            self.placePhotoModel = [[DesPlacePhotoObject alloc] initWithDic:photoDic];
            [self.placePhotoArr addObject:self.placePhotoModel];
            [_placePhotoModel release];
        }
        self.detailModel.hottest_places = self.placePhotoArr;
        [self handlePhotosData];
        [_detailModel release];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        id resultID = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        
        self.detailModel = [[DestinationDetailObject alloc] initWithDic:resultID];
        self.detailModelArr = [NSMutableArray array];
        [self.detailModelArr addObject:self.detailModel];
        NSMutableArray *placesArr = [resultID objectForKey:@"hottest_places"];
        self.placePhotoArr = [NSMutableArray array];
        for (NSMutableDictionary *photoDic in placesArr) {
            self.placePhotoModel = [[DesPlacePhotoObject alloc] initWithDic:photoDic];
            [self.placePhotoArr addObject:self.placePhotoModel];
            [_placePhotoModel release];
        }
        self.detailModel.hottest_places = self.placePhotoArr;
        [self setupViews];
        [_detailModel release];
        static NSInteger i = 1;
        if (1 == i) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力,请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            [alertView release];
        }
    }];
}
- (void)handlePhotosData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/photos/?start=0&count=21&gallery_mode=true", self.placeSendType, self.placeSendID];
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [NSString stringWithFormat:@"%@/%ld.happyTripPlacePhotos", docPath, [urlStr hash]];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        BOOL result = [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
        NSLog(@"存储成功: %d", result);
        NSMutableArray *itemsArr = [NSMutableArray array];
        itemsArr = [responseObject objectForKey:@"items"];
        self.smallPhoto_albumArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in itemsArr) {
            DesSmallPhoto_albumObject *smallPhtots = [[DesSmallPhoto_albumObject alloc] initWithDic:dic];
            [self.smallPhoto_albumArr addObject:smallPhtots];
            [smallPhtots release];
        }
        if (_smallPhoto_albumArr.count > 4) {
            [self setupCollectionView];
        }
        [self makeViewsFrame];
        [self valueMethod];
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        id resultID = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        NSMutableArray *itemsArr = [NSMutableArray array];
        itemsArr = [resultID objectForKey:@"items"];
        self.smallPhoto_albumArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in itemsArr) {
            DesSmallPhoto_albumObject *smallPhtots = [[DesSmallPhoto_albumObject alloc] initWithDic:dic];
            [self.smallPhoto_albumArr addObject:smallPhtots];
            [smallPhtots release];
        }
        if (_smallPhoto_albumArr.count > 4) {
            [self setupCollectionView];
        }
        [self makeViewsFrame];
        [self valueMethod];
        [self.collectionView reloadData];
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
